//
//  MYLoginVC.m
//  MuYing
//
//  Created by tbb on 2019/7/18.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYLoginVC.h"
#import "MYLoginBaseVC.h"
#import "MYLoginListVC.h"
#import "MYRegisterListVC.h"
#import <JXCategoryView/JXCategoryView.h>
#import "YQMYBaseNavigationController.h"
#import "YQMYTabBarBaseViewController.h"
@interface MYLoginVC ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) MYLoginHeaderView * topView;
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@property (nonatomic, copy) NSArray * titles;

@end

@implementation MYLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(168);
    }];

}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)setupView {
    
    self.titles = @[kYQLS(@"登录"),kYQLS(@"新注册")];
    self.categoryView = [[JXCategoryTitleView alloc] init];
    self.categoryView.delegate = self;
    self.categoryView.titles = self.titles;
    self.categoryView.defaultSelectedIndex = 0;
    //    self.categoryView.cellSpacing = 82;
    self.categoryView.contentEdgeInsetLeft = 76;
    self.categoryView.contentEdgeInsetRight = 76;
    self.categoryView.titleSelectedColor = UIColor.whiteColor;//HexColor(0x4A99F5);
    self.categoryView.titleColor = UIColor.whiteColor;//HexColor(0x999999);
    self.categoryView.titleColorGradientEnabled = YES;
//    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.averageCellSpacingEnabled = YES;
    self.categoryView.titleFont =  [UIFont systemFontOfSize:17 weight:(UIFontWeightMedium)];
    [self.topView addSubview:self.categoryView];

    JXCategoryIndicatorTriangleView *triangleView = [[JXCategoryIndicatorTriangleView alloc] init];
    triangleView.triangleViewColor = UIColor.whiteColor;
    triangleView.triangleViewSize = CGSizeMake(7, 5);
    self.categoryView.indicators = @[triangleView];
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    self.listContainerView.defaultSelectedIndex = 0;
    [self.view addSubview:self.listContainerView];
    self.categoryView.contentScrollView = self.listContainerView.scrollView;
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, 128, self.view.bounds.size.width, 40);
    self.listContainerView.frame = CGRectMake(0, 168, self.view.bounds.size.width, self.view.bounds.size.height - 168);
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}
#pragma mark - JXCategoryViewDelegate end
#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    if (index == 0) {
        MYLoginListVC *loginListVC = [[MYLoginListVC alloc] init];
        loginListVC.showType = 0;
        [self addChildViewController:loginListVC];
        return loginListVC;
    }else {
        MYRegisterListVC *registerListVC = [[MYRegisterListVC alloc] init];
        registerListVC.showType = 1;
        [self addChildViewController:registerListVC];
        return registerListVC;
    }
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

#pragma mark - JXCategoryListContainerViewDelegate end

#pragma mark -- lazy
-(MYLoginHeaderView *)topView {
    if (!_topView) {
        _topView = [MYLoginHeaderView viewFromXib];
        ws(self);
        [_topView.backBtn addTapAction:^(id  _Nonnull sender) {
            if (![self isEqual:self.navigationController.childViewControllers.firstObject]) {
                [weakself.navigationController dismissViewControllerAnimated:true completion:nil];
            }else{
                YQMYTabBarBaseViewController *tabBarVC = [[YQMYTabBarBaseViewController alloc] init];
                [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
            }

        }];
    }
    return _topView;
}
#pragma mark -- lazy end
@end
